package me;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author yexy
 * @date 2020/6/15 9:18 上午
 * @description
 */
public class StudentManage {

    private ArrayList<Student> studentList;
    private static final String FILE_PATH = "./stuData.txt";

    public StudentManage() {
        File file = new File(FILE_PATH);
        ObjectInputStream objectInputStream = null;
        if (file.exists()) {
            try {
                objectInputStream = new ObjectInputStream(new FileInputStream(file));
                this.studentList = (ArrayList<Student>)objectInputStream.readObject();
                if (this.studentList == null || this.studentList.size() ==0){
                    this.studentList = new ArrayList<>();
                    studentList.add(new Student("1", "tom", 11));
                    studentList.add(new Student("2", "sam", 11));
                    studentList.add(new Student("3", "amy", 11));
                    studentList.add(new Student("4", "amy", 14));
                }

            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    objectInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    public void add(Student s) {
        this.studentList.add(s);
    }

    public void deleteByNo(String no) {
        Student oldStu = getByNo(no);
        this.studentList.remove(oldStu);
    }

    public Student getByNo(String no) {
        Student getS = null;
        for (Student student : studentList) {
            if (student.getNo().equals(no)) {
                getS = student;
                break;
            }

        }
        return getS;
    }

    public List<Student> queryByName(String name) {
        ArrayList<Student> getS = new ArrayList<>();
        for (Student student : studentList) {
            if (student.getName().equals(name))
                getS.add(student);
        }
        return getS;
    }

    public List<Student> queryByAge(Integer age) {
        ArrayList<Student> getS = new ArrayList<>();
        for (Student student : studentList) {
            if (student.getAge().equals(age))
                getS.add(student);
        }
        return getS;
    }

    public void modifyByNo(String no, Student s) throws AgeException, StuNoException {
        Student oldStu = getByNo(no);
        oldStu.setNo(s.getNo());
        oldStu.setName(s.getName());
        oldStu.setAge(s.getAge());
    }

    public String show() {
        String result = "";
        for (Student student : studentList) {
            result += student.toString() + "\n";
        }
        return result;
    }

    public String show(List<Student> list) {
        String result = "";
        for (Student student : list) {
            result += student.toString() + "\n";
        }
        return result;
    }

    public void serializable() {
        ObjectOutputStream objectOutputStream = null;
        try {
            File file = new File(FILE_PATH);
            System.out.println(file.getAbsolutePath());
            objectOutputStream = new ObjectOutputStream(new FileOutputStream(file));
            objectOutputStream.writeObject(this.studentList);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (objectOutputStream != null) {
                try {
                    objectOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}
